package com.sjzd.meeting.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjzd.meeting.dao.MeetingGrabDAO;
import com.sjzd.meeting.pojo.MeetingGrab;
import com.sjzd.meeting.pojo.MeetingPublish;
import com.sjzd.meeting.service.IMeetingGrabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class MeetingGrabServiceImpl implements IMeetingGrabService {

    @Autowired
    private MeetingGrabDAO meetingGrabDAO;

    @Override
    public int save(MeetingGrab meetingGrab) {
        return meetingGrabDAO.insert(meetingGrab);
    }

    @Override
    public PageInfo<MeetingPublish> selectByUidAndTid(int uid, int tid, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeetingPublish> meetingPublishes = meetingGrabDAO.selectByUidAndTid(uid, tid);
        PageInfo<MeetingPublish> pageInfo = new PageInfo<>(meetingPublishes, pageSize);
        return pageInfo;
    }

    @Override
    public PageInfo<MeetingGrab> selectByUid(int uid, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeetingGrab> meetingGrabs = meetingGrabDAO.selectByUid(uid);
        PageInfo<MeetingGrab> pageInfo = new PageInfo<>(meetingGrabs, pageSize);
        return pageInfo;
    }

    @Transactional
    @Override
    public int confirmCandidate(String id, String mid) {
        // 修改成功匹配的编号订单信息
        MeetingGrab meetingGrab = new MeetingGrab();
        meetingGrab.setId(id);
        meetingGrab.setMid(mid);
        meetingGrab.setMatchStatus(1);
        meetingGrab.setMatchTime(new Date());
        int num1 = meetingGrabDAO.updateByIdAndMid(meetingGrab);
        // 修改未成功匹配的编号订单信息
        int num2 = meetingGrabDAO.updateFailOrderMatchStatusByIdAndMid(id, mid);
        if ((num1 + num2) >= 1) {
            return 2;
        } else {
            return 1;
        }
    }

    @Override
    public PageInfo<MeetingGrab> selectCandidateByMid(String mid, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeetingGrab> meetingPublishes = meetingGrabDAO.selectCandidateByMid(mid);
        PageInfo<MeetingGrab> pageInfo = new PageInfo<>(meetingPublishes, pageSize);
        return pageInfo;
    }
}
